import zmq
import sys
from cgi import escape

query = ""
for i in range(1, len(sys.argv)):
	query = query + sys.argv[i] + " "
	
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect ("tcp://localhost:5555") #connect to C++ search server database
socket.send (query)

num_search_result = socket.recv() # Get the total number of search result

search_result = []

if int(num_search_result) == 0 :
	search_result.append("NO RESULT FIND\n")
	search_result.append("\\textbf{Number of result} = 0")

else :
	for i in range (0, int(num_search_result)) :
		socket.send("I got it!")
		result = socket.recv()
		search_result.append(result)

RESULT = open("D:\\equation_search\\server\\result", "w")
RESULT.writelines(search_result) # writes search result to result file. 
RESULT.close()
